const { getOrderList, getOrderCount, getOrderInfo, removeOrder, addOrder, updateOrder } = require('../../model/order')
const { getMemberInfoById } = require('../../model/member')
/**
 * 获取订单列表 
*/
const orderList = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const { id: uid } = req.loginInfo
    const { page, size } = req.query
    const map = page && size ? { page, size } : {}
    map.uid = uid
    const result = await getOrderList(map)
    output.data = result
    return res.send(output)
}

/**
 * 获取订单总数 
*/
const orderTotal = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    const { id: uid } = req.loginInfo
    map.uid = uid
    const result = await getOrderCount(map)
    output.data = result
    return res.send(output)
}

/**
 * 新增订单总
*/
const orderAdd = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    // 登录账号id
    const { id: uid, account_type } = req.loginInfo
    console.log(account_type,'account_type');
    
    if (account_type !== 3) {
        output.code = 500
        output.msg = '账号类型错误:非企业账号,无法进行此项操作'
        return res.send(output)
    }
    // 根据账号id查询企业id
    const { bus_id } = await getMemberInfoById(uid)
    if (!bus_id) {
        output.code = 500
        output.msg = '系统数据错误:未查询到企业信息'
        return res.send(output)
    }
    // 订单数据: product:认证年份
    const data = { ...req.body, uid, bus_id }
    const result = await addOrder(data)
    output.data = result
    return res.send(output)
}

/**
 * 更新订单(支付)状态: 支付平台异步通知地址
*/
const orderUpdate = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    return res.send(output)
}

/**
 * 获取订单详情
 *  
 * */
const orderDetail = async (req, res) => {
    const output = { code: 200, msg: '操作成功' }
    // 获取订单id
    const { id } = req.query
    if (!id) {
        output.code = 500
        output.msg = '参数错误:缺少参数id'
        return res.send(output)
    }
    const result = await getOrderInfo({ id })
    output.data = result
    res.send(output)
}

module.exports = {
    orderList, orderDetail, orderTotal, orderAdd, orderUpdate
}